package com.becca.cake.controller.goods;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.core.common.PagingBean;
import com.becca.cake.core.exception.CommonExceptionHandle;
import com.becca.cake.model.coupon.GoodsActivityModel;
import com.becca.cake.model.goods.GoodsLableModel;
import com.becca.cake.model.goods.GoodsModel;
import com.becca.cake.model.goods.GoodsPictureModel;
import com.becca.cake.model.goods.GoodsPriceSizeModel;
import com.becca.cake.service.goods.IGoodsService;
import com.becca.cake.util.JsonUtil;

/**
 * 商品管理controller类
 * @copyright  尊星(上海)信息科技有限公司
 * @author yellow
 * @date 2016年5月18日 下午9:30:52
 */
@Controller
@RequestMapping("/goodsManager")
public class GoodsAction extends BaseAction {
    private final static Log logger = LogFactory.getLog(GoodsAction.class);

    @Autowired
    private IGoodsService    iGoodsService;

    /**
     * 查询商品数据
     * @param goodsModel
     * @return
     */
    @RequestMapping(value = "/selectAllGoods", method = RequestMethod.POST)
    @ResponseBody
    public String selectGoods(GoodsModel goodsModel,
                              @RequestParam(value = "existall", required = false) String existall,
                              @RequestParam(value = "statusArr", required = false) String statusArr) {

        Map<String, Object> map = new HashMap<String, Object>();
        PagingBean page = getInitPagingBean();
        map.put("pagingBean", page);
        map.put("goodsModel", goodsModel);
        goodsModel.setGoodsType(GoodsModel.GOODS_TYPE_DEFALUT);
        try {
            // stmp1:查询商品数据
            List<GoodsModel> list = this.iGoodsService.selectGoods(map);
            if ("true".equals(existall)) {
                GoodsModel s = new GoodsModel();
                s.setGoodsName("全部");
                list.add(s);
            }
            // stmp2:获取商品总条数
            Long count = this.iGoodsService.selectGoodsCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 添加或修改商品信息
     * @author yellow
     * @return
     */
    @RequestMapping(value = "/insertOrUpdateGoods", method = RequestMethod.POST)
    @ResponseBody
    public String insertOrUpdateGoods(HttpServletRequest request, GoodsModel goodsModel) {
        //step1:获取图片file
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile goodsBasePicFile = multipartRequest.getFile("goodsBasePic_upload");
        MultipartFile goodsPicFile = multipartRequest.getFile("goodsPic_upload");
        List<MultipartFile> files = new ArrayList<MultipartFile>();
        files.add(goodsPicFile);
        files.add(goodsBasePicFile);
        try {
            this.iGoodsService.insertOrUpdateGoods(goodsModel, files, getFreeMarketFoderPath(),
                getTempFoderPath(), this.getUserSeesion());
            return JsonUtil.returnJsonSuccess("保存商品信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 下架商品
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/updGoodsStatusById", method = RequestMethod.POST)
    @ResponseBody
    public String updGoodsStatusById(@RequestParam(value = "goodsId", required = false) Integer goodsId,
                                     @RequestParam(value = "goodsStatus", required = false) Integer goodsStatus) {
        try {
            this.iGoodsService.updGoodsStatusById(goodsId, goodsStatus, this.getUserSeesion()
                .getTrueName());
            return JsonUtil.returnJsonSuccess("下架商品成功！");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据商品ID查询商品对象
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/selectGoodsById", method = RequestMethod.POST)
    @ResponseBody
    public String selectGoodsById(@RequestParam("goodsId") Integer goodsId) {
        try {
            GoodsModel goodsModel = this.iGoodsService.selectGoodsById(goodsId);
            return JsonUtil.writeObjectFormJSON(goodsModel);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据商品ID查询尺寸大小列表
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/queryGoodsPriceSizeByGoodsId", method = RequestMethod.POST)
    @ResponseBody
    public String queryGoodsPriceSizeByGoodsId(@RequestParam(value = "goodsId", required = false) Integer goodsId) {
        Map<String, Object> map = new HashMap<String, Object>();
        PagingBean page = getInitPagingBean();
        map.put("goodsId", goodsId);
        map.put("pagingBean", page);
        map.put("isDelete", 0);
        try {
            List<GoodsPriceSizeModel> list = this.iGoodsService.queryGoodsPriceSizeByGoodsId(map);
            Long count = this.iGoodsService.queryGoodsPriceSizeCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 添加或修改商品尺寸信息
     * @author yellow
     * @return
     */
    @RequestMapping(value = "/addOrUpdGoodsPriceSize", method = RequestMethod.POST)
    @ResponseBody
    public String addOrUpdGoodsPriceSize(GoodsPriceSizeModel goodsPriceSizeModel) {
        try {
            this.iGoodsService.addOrUpdGoodsPriceSize(goodsPriceSizeModel, this.getUserSeesion()
                .getTrueName());
            return JsonUtil.returnJsonSuccess("保存商品尺寸信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据商品尺寸ID查询商品尺寸对象
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/selectGoodsPriceSizeUpdById", method = RequestMethod.POST)
    @ResponseBody
    public String selectGoodsPriceSizeUpdById(@RequestParam("id") Integer id) {
        try {
            GoodsPriceSizeModel goodsPriceSizeModel = this.iGoodsService
                .selectGoodsPriceSizeModelById(id);
            return JsonUtil.writeObjectFormJSON(goodsPriceSizeModel);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据id修改isDelete
     * @param id
     * @return
     */
    @RequestMapping(value = "/updateIsDelete", method = RequestMethod.POST)
    @ResponseBody
    public String updateIsDelete(@RequestParam(value = "id", required = false) Integer id,
                                 @RequestParam(value = "isDelete", required = false) Integer isDelete) {
        try {
            this.iGoodsService.updIsDelete(id, isDelete, this.getUserSeesion().getName());
            return JsonUtil.returnJsonSuccess("操作商品尺寸信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 添加或修改图片信息
     * @author yellow
     * @return
     */
    @RequestMapping(value = "/addOrUpdGoodsPic", method = RequestMethod.POST)
    @ResponseBody
    public String addOrUpdGoodsPic(HttpServletRequest request, GoodsPictureModel goodsPictureModel) {
        //step1:获取图片file
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile files = multipartRequest.getFile("upload");
        try {
            this.iGoodsService.insertOrUpdateGoodsPic(goodsPictureModel, files, getTempFoderPath(),
                this.getUserSeesion());
            return JsonUtil.returnJsonSuccess("操作商品图片信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据商品图片ID查询商品图片对象
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/selectGoodsPicById", method = RequestMethod.POST)
    @ResponseBody
    public String selectGoodsPicById(@RequestParam("id") Integer id) {
        try {
            GoodsPictureModel goodsPictureModel = this.iGoodsService.selectGoodsPicById(id);
            return JsonUtil.writeObjectFormJSON(goodsPictureModel);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据id修改图片isDelete
     * @param id
     * @return
     */
    @RequestMapping(value = "/updPicIsDelete", method = RequestMethod.POST)
    @ResponseBody
    public String updPicIsDelete(@RequestParam(value = "id", required = false) Integer id,
                                 @RequestParam(value = "isDelete", required = false) Integer isDelete) {
        try {
            this.iGoodsService.updGoodsPicIsDelete(id, isDelete, this.getUserSeesion()
                .getTrueName());
            return JsonUtil.returnJsonSuccess("操作商品尺寸信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据商品ID查询商品图片列表
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/queryGoodsPics", method = RequestMethod.POST)
    @ResponseBody
    public String queryGoodsPics(GoodsPictureModel goodsPictureModel,
                                 @RequestParam(value = "goodsCode", required = false) String goodsCode) {
        Map<String, Object> map = new HashMap<String, Object>();
        PagingBean page = getInitPagingBean();
        map.put("goodsCode", goodsCode);
        map.put("goodsPictureModel", goodsPictureModel);
        map.put("pagingBean", page);
        map.put("isDelete", 0);
        try {
            List<GoodsPictureModel> list = this.iGoodsService.queryGoodsPics(map);
            Long count = this.iGoodsService.queryGoodsPicCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 上传图片文件
     * @param request
     * @param subjectModel
     * @return
     */
    @RequestMapping(value = "/uploadPictureFile", method = RequestMethod.POST)
    @ResponseBody
    public String uploadPictureFile(HttpServletRequest request) {
        //step1:获取file
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile files = multipartRequest.getFile("filePic");
        try {
            //step2:上传图片文件
            String filepath = this.iGoodsService.uploadPictureFile(files, getTempFoderPath());
            return JsonUtil.returnJsonSuccess(filepath);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 批量修改Order排序列
     * @param advertId
     * @return
     */
    @RequestMapping(value = "/updateGoodsPicOrder", method = RequestMethod.POST)
    @ResponseBody
    public String updateGoodsPicOrder(@RequestParam(value = "jsonList", required = false) String jsonList) {
        try {
            this.iGoodsService.updateGoodsPicOrders(jsonList);
            return JsonUtil.returnJsonSuccess("操作商品图片信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 查询商品活动列表
     * @param paymentModel
     * @return
     */
    @RequestMapping(value = "/selectGoodsActivity", method = RequestMethod.POST)
    @ResponseBody
    public String selectGoodsActivity(GoodsActivityModel goodsActivityModel) {
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("goodsActivityModel", goodsActivityModel);
        map.put("pagingBean", page);
        try {
            // step1:查询商品活动数据
            List<GoodsActivityModel> list = this.iGoodsService.selectGoodsActivity(map);
            // step2:获取商品活动总条数
            Long count = this.iGoodsService.selectGoodsActivityCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 添加或修改商品活动信息
     * @param goodsActivityModel
     * @return
     */
    @RequestMapping(value = "/addOrUpdGoodsActivity", method = RequestMethod.POST)
    @ResponseBody
    public String addOrUpdGoodsActivity(GoodsActivityModel goodsActivityModel) {
        try {
            this.iGoodsService.insertOrUpdateGoodsActivity(goodsActivityModel, this
                .getUserSeesion().getTrueName());
            return JsonUtil.returnJsonSuccess("保存商品活动信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据id查询商品活动对象
     * @param id
     * @return
     */
    @RequestMapping(value = "/selectGoodsActivityUpdById", method = RequestMethod.POST)
    @ResponseBody
    public String selectGoodsActivityUpdById(@RequestParam("id") Integer id) {
        try {
            GoodsActivityModel goodsActivityModel = this.iGoodsService
                .selectGoodsActivityUpdById(id);
            return JsonUtil.writeObjectFormJSON(goodsActivityModel);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 批量修改商品Order排序列
     * @param advertId
     * @return
     */
    @RequestMapping(value = "/updateGoodsOrder", method = RequestMethod.POST)
    @ResponseBody
    public String updateGoodsOrder(@RequestParam(value = "jsonList", required = false) String jsonList) {
        try {
            this.iGoodsService.updateGoodsOrder(jsonList, this.getUserSeesion().getTrueName());
            return JsonUtil.returnJsonSuccess("批量修改商品排序成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 商品标签查询
     * @param goodsLableModel
     * @return
     */
    @RequestMapping(value = "/selectGoodsLable", method = RequestMethod.POST)
    @ResponseBody
    public String selectGoodsLable(GoodsLableModel goodsLableModel){
    	PagingBean page = getInitPagingBean();
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("goodsLableModel", goodsLableModel);
    	map.put("pagingBean", page);
    	try
		{
    		List<GoodsLableModel> list = this.iGoodsService.selectGoodsLable(map);
        	Long count = this.iGoodsService.selectGoodsLableCount(map);
        	return JsonUtil.writeObjectPageJSON(list, count);
		} catch (Exception e)
		{
			return JsonUtil.returnJsonUnSuccess("查询标签信息失败!");
		}
    	
    }
    
    /**
     * 添加或修改标签信息
     * @param goodsLableModel
     * @return
     */
    @RequestMapping(value = "/addOrUpdateGoodsLable", method = RequestMethod.POST)
    @ResponseBody
    public String addOrUpdateGoodsLable(HttpServletRequest request, GoodsLableModel goodsLableModel) {
        try {
            //step2:添加或修改主题信息
            this.iGoodsService.addOrUpdateGoodsLable(goodsLableModel,this.getUserSeesion());
            return JsonUtil.returnJsonSuccess("操作标签信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 根据id移除主题商品标签
     * @param subGoodsId
     * @return
     */
    @RequestMapping(value = "/deleteGoodsLableById", method = RequestMethod.POST)
    @ResponseBody
    public String deleteGoodsLableById(@RequestParam("goodsLableId") Integer id) {
        try {
            this.iGoodsService.deleteGoodsLableById(id);
            return JsonUtil.returnJsonSuccess("移除商品标签成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }

    }
    
    /**
     * 根据商品标签ID查询商品标签对象
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/selectGoodsLableById", method = RequestMethod.POST)
    @ResponseBody
    public String selectGoodsLableById(@RequestParam("id") Integer id) {
        try {
        	GoodsLableModel goodsLableModel = this.iGoodsService.selectGoodsLableById(id);
            return JsonUtil.writeObjectFormJSON(goodsLableModel);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    

}
